home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / swingall.jar / javax / swing / text / html / HTMLEditorKit.class (.txt) < prev    next >
Encoding:
Java Class File  |  1999-07-15  |  10.2 KB  |  246 lines

  1. package javax.swing.text.html;
  2.  
  3. import java.awt.Component;
  4. import java.awt.event.MouseListener;
  5. import java.awt.event.MouseMotionListener;
  6. import java.io.BufferedReader;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.io.Reader;
  11. import java.io.StringReader;
  12. import java.io.Writer;
  13. import java.lang.reflect.Method;
  14. import java.net.URL;
  15. import javax.swing.Action;
  16. import javax.swing.JEditorPane;
  17. import javax.swing.text.AbstractDocument;
  18. import javax.swing.text.BadLocationException;
  19. import javax.swing.text.Document;
  20. import javax.swing.text.Element;
  21. import javax.swing.text.MutableAttributeSet;
  22. import javax.swing.text.Style;
  23. import javax.swing.text.StyleConstants;
  24. import javax.swing.text.StyledDocument;
  25. import javax.swing.text.StyledEditorKit;
  26. import javax.swing.text.TextAction;
  27. import javax.swing.text.ViewFactory;
  28. import javax.swing.text.html.HTML.Attribute;
  29. import javax.swing.text.html.HTML.Tag;
  30.  
  31. public class HTMLEditorKit extends StyledEditorKit {
  32.    public static final String DEFAULT_CSS = "default.css";
  33.    MutableAttributeSet input;
  34.    private static StyleSheet defaultStyles = null;
  35.    private MouseListener linkHandler = new LinkController();
  36.    private static Parser defaultParser = null;
  37.    private MouseMotionListener tmpHandler = new TemporaryHandler();
  38.    public static final String BOLD_ACTION = "html-bold-action";
  39.    public static final String ITALIC_ACTION = "html-italic-action";
  40.    public static final String PARA_INDENT_LEFT = "html-para-indent-left";
  41.    public static final String PARA_INDENT_RIGHT = "html-para-indent-right";
  42.    public static final String FONT_CHANGE_BIGGER = "html-font-bigger";
  43.    public static final String FONT_CHANGE_SMALLER = "html-font-smaller";
  44.    public static final String COLOR_ACTION = "html-color-action";
  45.    public static final String LOGICAL_STYLE_ACTION = "html-logical-style-action";
  46.    public static final String IMG_ALIGN_TOP = "html-image-align-top";
  47.    public static final String IMG_ALIGN_MIDDLE = "html-image-align-middle";
  48.    public static final String IMG_ALIGN_BOTTOM = "html-image-align-bottom";
  49.    public static final String IMG_BORDER = "html-image-border";
  50.    private static final String INSERT_TABLE_HTML = "<table border=1><tr><td></td></tr></table>";
  51.    private static final String INSERT_UL_HTML = "<ul><li></li></ul>";
  52.    private static final String INSERT_OL_HTML = "<ol><li></li></ol>";
  53.    private static final String INSERT_HR_HTML = "<hr>";
  54.    private static final String INSERT_PRE_HTML = "<pre></pre>";
  55.    private static final Action[] defaultActions;
  56.    // $FF: synthetic field
  57.    static Class class$javax$swing$text$html$HTMLEditorKit;
  58.    // $FF: synthetic field
  59.    static Class class$java$lang$String;
  60.  
  61.    static {
  62.       defaultActions = new Action[]{new InsertHTMLTextAction("InsertTable", "<table border=1><tr><td></td></tr></table>", Tag.BODY, Tag.TABLE), new InsertHTMLTextAction("InsertTableRow", "<table border=1><tr><td></td></tr></table>", Tag.TABLE, Tag.TR, Tag.BODY, Tag.TABLE), new InsertHTMLTextAction("InsertTableDataCell", "<table border=1><tr><td></td></tr></table>", Tag.TR, Tag.TD, Tag.BODY, Tag.TABLE), new InsertHTMLTextAction("InsertUnorderedList", "<ul><li></li></ul>", Tag.BODY, Tag.UL), new InsertHTMLTextAction("InsertUnorderedListItem", "<ul><li></li></ul>", Tag.UL, Tag.LI, Tag.BODY, Tag.UL), new InsertHTMLTextAction("InsertOrderedList", "<ol><li></li></ol>", Tag.BODY, Tag.OL), new InsertHTMLTextAction("InsertOrderedListItem", "<ol><li></li></ol>", Tag.OL, Tag.LI, Tag.BODY, Tag.OL), new InsertHRAction(), new InsertHTMLTextAction("InsertPre", "<pre></pre>", Tag.BODY, Tag.PRE)};
  63.    }
  64.  
  65.    // $FF: synthetic method
  66.    static Class class$(String var0) {
  67.       try {
  68.          return Class.forName(var0);
  69.       } catch (ClassNotFoundException var2) {
  70.          throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  71.       }
  72.    }
  73.  
  74.    public Object clone() {
  75.       return new HTMLEditorKit();
  76.    }
  77.  
  78.    public Document createDefaultDocument() {
  79.       StyleSheet var1 = this.getStyleSheet();
  80.       StyleSheet var2 = new StyleSheet();
  81.       var2.addStyleSheet(var1);
  82.       HTMLDocument var3 = new HTMLDocument(var2);
  83.       ((AbstractDocument)var3).putProperty(HTMLDocument.PARSER_PROPERTY, this.getParser());
  84.       ((AbstractDocument)var3).setAsynchronousLoadPriority(4);
  85.       var3.setTokenThreshold(100);
  86.       return var3;
  87.    }
  88.  
  89.    protected void createInputAttributes(Element var1, MutableAttributeSet var2) {
  90.       var2.removeAttributes(var2);
  91.       var2.addAttributes(var1.getAttributes());
  92.       var2.removeAttribute(StyleConstants.ComposedTextAttribute);
  93.       Object var3 = var2.getAttribute(StyleConstants.NameAttribute);
  94.       if (var3 instanceof HTML.Tag) {
  95.          HTML.Tag var4 = (HTML.Tag)var3;
  96.          if (var4 == Tag.IMG) {
  97.             var2.removeAttribute(Attribute.SRC);
  98.             var2.removeAttribute(Attribute.HEIGHT);
  99.             var2.removeAttribute(Attribute.WIDTH);
  100.             var2.addAttribute(StyleConstants.NameAttribute, Tag.CONTENT);
  101.          } else if (var4 == Tag.HR) {
  102.             var2.addAttribute(StyleConstants.NameAttribute, Tag.CONTENT);
  103.          } else if (var4 == Tag.COMMENT) {
  104.             var2.addAttribute(StyleConstants.NameAttribute, Tag.CONTENT);
  105.             var2.removeAttribute(Attribute.COMMENT);
  106.          } else if (var4 instanceof HTML.UnknownTag) {
  107.             var2.addAttribute(StyleConstants.NameAttribute, Tag.CONTENT);
  108.             var2.removeAttribute(Attribute.ENDTAG);
  109.          }
  110.       }
  111.  
  112.    }
  113.  
  114.    public void deinstall(JEditorPane var1) {
  115.       ((Component)var1).removeMouseListener(this.linkHandler);
  116.       ((Component)var1).removeMouseMotionListener(this.tmpHandler);
  117.       super.deinstall(var1);
  118.    }
  119.  
  120.    public Action[] getActions() {
  121.       return TextAction.augmentList(super.getActions(), defaultActions);
  122.    }
  123.  
  124.    public String getContentType() {
  125.       return "text/html";
  126.    }
  127.  
  128.    public MutableAttributeSet getInputAttributes() {
  129.       if (this.input == null) {
  130.          this.input = this.getStyleSheet().addStyle((String)null, (Style)null);
  131.       }
  132.  
  133.       return this.input;
  134.    }
  135.  
  136.    protected Parser getParser() {
  137.       if (defaultParser == null) {
  138.          try {
  139.             Class var1 = Class.forName("javax.swing.text.html.parser.ParserDelegator");
  140.             defaultParser = (Parser)var1.newInstance();
  141.          } catch (Throwable var2) {
  142.          }
  143.       }
  144.  
  145.       return defaultParser;
  146.    }
  147.  
  148.    static InputStream getResourceAsStream(String var0) {
  149.       try {
  150.          ClassLoader var2 = (class$javax$swing$text$html$HTMLEditorKit != null ? class$javax$swing$text$html$HTMLEditorKit : (class$javax$swing$text$html$HTMLEditorKit = class$("javax.swing.text.html.HTMLEditorKit"))).getClassLoader();
  151.          Class var1;
  152.          if (var2 != null) {
  153.             var1 = var2.loadClass("javax.swing.text.html.ResourceLoader");
  154.          } else {
  155.             var1 = Class.forName("javax.swing.text.html.ResourceLoader");
  156.          }
  157.  
  158.          Class[] var3 = new Class[]{class$java$lang$String != null ? class$java$lang$String : (class$java$lang$String = class$("java.lang.String"))};
  159.          Method var4 = var1.getMethod("getResourceAsStream", var3);
  160.          String[] var5 = new String[]{var0};
  161.          return (InputStream)var4.invoke((Object)null, var5);
  162.       } catch (Throwable var6) {
  163.          return (class$javax$swing$text$html$HTMLEditorKit != null ? class$javax$swing$text$html$HTMLEditorKit : (class$javax$swing$text$html$HTMLEditorKit = class$("javax.swing.text.html.HTMLEditorKit"))).getResourceAsStream(var0);
  164.       }
  165.    }
  166.  
  167.    public StyleSheet getStyleSheet() {
  168.       if (defaultStyles == null) {
  169.          defaultStyles = new StyleSheet();
  170.  
  171.          try {
  172.             InputStream var1 = getResourceAsStream("default.css");
  173.             BufferedReader var2 = new BufferedReader(new InputStreamReader(var1));
  174.             defaultStyles.loadRules(var2, (URL)null);
  175.             ((Reader)var2).close();
  176.          } catch (Throwable var3) {
  177.          }
  178.       }
  179.  
  180.       return defaultStyles;
  181.    }
  182.  
  183.    public ViewFactory getViewFactory() {
  184.       return new HTMLFactory();
  185.    }
  186.  
  187.    public void insertHTML(HTMLDocument var1, int var2, String var3, int var4, int var5, HTML.Tag var6) throws BadLocationException, IOException {
  188.       Parser var7 = this.getParser();
  189.       if (var7 == null) {
  190.          throw new IOException("Can't load parser");
  191.       } else if (var2 > ((AbstractDocument)var1).getLength()) {
  192.          throw new BadLocationException("Invalid location", var2);
  193.       } else {
  194.          ParserCallback var8 = var1.getReader(var2, var4, var5, var6);
  195.          Boolean var9 = (Boolean)((AbstractDocument)var1).getProperty("IgnoreCharsetDirective");
  196.          var7.parse(new StringReader(var3), var8, var9 == null ? false : var9);
  197.          var8.flush();
  198.       }
  199.    }
  200.  
  201.    public void install(JEditorPane var1) {
  202.       ((Component)var1).addMouseListener(this.linkHandler);
  203.       ((Component)var1).addMouseMotionListener(this.tmpHandler);
  204.       super.install(var1);
  205.    }
  206.  
  207.    public void read(Reader var1, Document var2, int var3) throws IOException, BadLocationException {
  208.       if (var2 instanceof HTMLDocument) {
  209.          HTMLDocument var4 = (HTMLDocument)var2;
  210.          Parser var5 = this.getParser();
  211.          if (var5 == null) {
  212.             throw new IOException("Can't load parser");
  213.          }
  214.  
  215.          if (var3 > var2.getLength()) {
  216.             throw new BadLocationException("Invalid location", var3);
  217.          }
  218.  
  219.          ParserCallback var6 = var4.getReader(var3);
  220.          Boolean var7 = (Boolean)var2.getProperty("IgnoreCharsetDirective");
  221.          var5.parse(var1, var6, var7 == null ? false : var7);
  222.          var6.flush();
  223.       } else {
  224.          super.read(var1, var2, var3);
  225.       }
  226.  
  227.    }
  228.  
  229.    public void setStyleSheet(StyleSheet var1) {
  230.       defaultStyles = var1;
  231.    }
  232.  
  233.    public void write(Writer var1, Document var2, int var3, int var4) throws IOException, BadLocationException {
  234.       if (var2 instanceof HTMLDocument) {
  235.          HTMLWriter var5 = new HTMLWriter(var1, (HTMLDocument)var2, var3, var4);
  236.          var5.write();
  237.       } else if (var2 instanceof StyledDocument) {
  238.          MinimalHTMLWriter var6 = new MinimalHTMLWriter(var1, (StyledDocument)var2, var3, var4);
  239.          var6.write();
  240.       } else {
  241.          super.write(var1, var2, var3, var4);
  242.       }
  243.  
  244.    }
  245. }
  246.